package com.example.ktapplication.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.ktapplication.AppGlobals
import com.example.ktapplication.R
import com.example.ktapplication.adapter.RightNavigationAdapter.ContentViewHolder
import com.example.ktapplication.bean.Article
import com.example.ktapplication.bean.NavData
import com.example.ktapplication.utils.DpUtils
import com.example.ktapplication.utils.SpacesItemDecoration
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent

class RightNavigationAdapter(context: Context): RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private lateinit var mContext:Context

    var rightList = mutableListOf<NavData>()
    private lateinit var adapter:CommomAdapter

    var navItemClickAction:(Article)->Unit={}

    init {
        mContext=context
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(mContext)
            .inflate(R.layout.rv_right_item_layout, parent, false)
        return ContentViewHolder(view)
    }

    override fun getItemCount(): Int {
       return rightList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val bean: List<Article> = rightList.get(position).articles
        val viewHolder = holder as ContentViewHolder

        val flexboxLayoutManager = FlexboxLayoutManager(mContext)
        flexboxLayoutManager.setFlexDirection(FlexDirection.ROW);
        flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP)
        flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START);
        viewHolder.rvItem!!.layoutManager=flexboxLayoutManager
        adapter=CommomAdapter(mContext,bean,R.layout.nav_rv_itme_item)
        viewHolder.rvItem!!.adapter=adapter
        adapter.notifyDataSetChanged()
        adapter.setOnItemClickListener(CommomAdapter.OnItemClickListener { position ->
            navItemClickAction.invoke(bean[position])
        })
    }

    //内容 ViewHolder
    class ContentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val rvItem: RecyclerView = itemView.findViewById<RecyclerView>(R.id.rvItem)
        init {
            rvItem!!.addItemDecoration(
                SpacesItemDecoration(DpUtils.getInstance().px2dp(AppGlobals.getApplication(),16F))
            )
        }
    }
}

class CommomAdapter(context: Context, list: List<Article>, layoutId: Int)
    : RecyclerView.Adapter<CommomViewHolder>() {

    lateinit var mContext:Context

    lateinit var mDataList:List<Article>

    var mLayoutId:Int=0

    init {
        this.mContext=context
        this.mDataList=list
        this.mLayoutId=layoutId
    }

    private var mOnItemClickListener: OnItemClickListener? = null


    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        this.mOnItemClickListener = onItemClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommomViewHolder {
        val view = View.inflate(mContext, mLayoutId, null)
        return CommomViewHolder(view)
    }

    override fun getItemCount(): Int {
        return if (mDataList == null) 0 else mDataList.size
    }

    override fun onBindViewHolder(holder: CommomViewHolder, position: Int) {
        val viewHolder = holder as CommomViewHolder
        viewHolder.tvNavItem.text=mDataList[position].title
        setListener(holder, position);
    }


    private fun setListener(holder: CommomViewHolder, position: Int) {
        holder.itemView.setOnClickListener { view ->
            if (mOnItemClickListener != null) {
                mOnItemClickListener!!.onItemClick(position)
            }
        }
    }

    fun interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setData(list:List<Article>){
        mDataList=list
        notifyDataSetChanged()
    }

}

class CommomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    public  var tvNavItem:TextView=itemView.findViewById<TextView>(R.id.tvNavItem)

}